<template>
    <basic-container>
        <avue-crud ref="crud" :option="tableOption" :page.sync="page" :table-loading="listLoading"
                   :data="list" @on-load="getList"
                   @search-change="searchChange"
                   @refresh-change="refreshChange">

            <template slot="menuLeft" slot-scope="scope">
                <el-button type="primary" icon="el-icon-plus"
                           @click="handleSend()">发送平台消息
                </el-button>
            </template>
            <template slot="menu" slot-scope="scope">
                <el-button type="text" size="mini" icon="el-icon-view"
                           @click="handleDetail(scope.row)">详情
                </el-button>
                <el-button type="text" size="mini" icon="el-icon-edit"
                           v-show="scope.row.msgActionType === '0'"
                           @click="handleModify(scope.row)">编辑
                </el-button>
            </template>
        </avue-crud>
    </basic-container>

</template>

<script>
    import {fetchList} from '../../api/message/in_mail'
    import {tableOption} from '../../const/crud/message/message_record'

    export default {
        name: 'inMail',
        data() {
            return {
                tableOption: tableOption,
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20, // 每页显示多少条,
                    isAsc: false// 是否倒序
                },
                list: [],
                listLoading: true,
            }
        },
        created() {
            this.getHeight();
        },
        methods: {
            getHeight() {
                let tableHeight = Math.max(window.innerHeight - 400, 300);
                this.tableOption.maxHeight = tableHeight + 'px';
            },
            getList(page, params = {}) {
                this.listLoading = true;
                // 发送时间段处理
                if (params.sendTimeRange && params.sendTimeRange.length === 2) {
                    params.startDate = params.sendTimeRange[0];
                    params.endDate = params.sendTimeRange[1];
                }
                delete params.sendTimeRange;
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.list = response.data.data.records
                    this.page.total = response.data.data.total
                    this.listLoading = false
                })
            },
            handleDetail(row) {
                let query = {
                    res: {
                        messageSn: row.messageSn
                    }
                };
                this.$router.push({path: '/inMail/detail', query: query})
            },
            handleModify(row) {
                let query = {
                    res: {
                        messageSn: row.messageSn
                    }
                };
                this.$router.push({path: '/inMail/modify', query: query})
            },
            handleSend() {
                this.$router.push({path: '/inMail/sendPlatform'})
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
                this.getList(this.page, this.filterForm(info.searchForm));
            },
        }
    }
</script>

